/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plotter2d;

import de.congrace.exp4j.Calculable;
import de.congrace.exp4j.ExpressionBuilder;
import de.congrace.exp4j.UnknownFunctionException;
import de.congrace.exp4j.UnparsableExpressionException;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;

/**
 *
 * @author Bezdelnick
 */
public class PlotterApplet extends Applet {

    private List<GraphEntity> graphEntitys;
    private List<ColorEntity> colorEntitys;

    /**
     * Creates new form NewJPanel
     */
    public PlotterApplet() {
        //initComponents();
    }

    @Override
    public void init() {
        // TODO start asynchronous download of heavy resources
        initColors();
        initGraphs();
        initComponents();
        
        graphsListValueChanged(null);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g); //To change body of generated methods, choose Tools | Templates.
        try {
            renderGraphs();
        } catch (Exception ex) {
            Logger.getLogger(Plotter2dApplet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void initColors() {
        colorEntitys = new ArrayList<ColorEntity>();
        colorEntitys.add(new ColorEntity("Черный", Color.black));
        colorEntitys.add(new ColorEntity("Фиолетовый", Color.magenta));
        colorEntitys.add(new ColorEntity("Синий", Color.blue));
        colorEntitys.add(new ColorEntity("Серый", Color.gray));
        colorEntitys.add(new ColorEntity("Красный", Color.red));
        colorEntitys.add(new ColorEntity("Розовый", Color.pink));
        colorEntitys.add(new ColorEntity("Желтый", Color.yellow));
    }

    private void initGraphs() {
        graphEntitys = new ArrayList<GraphEntity>();
        graphEntitys.add(new GraphEntity("График 0", -20, 20, 0.5f, "3*x+7", colorEntitys.get(0)));
        graphEntitys.add(new GraphEntity("График 1", -20, 20, 0.5f, "x^2", colorEntitys.get(1)));
        graphEntitys.add(new GraphEntity("График 2", -20, 20, 0.5f, "2/x", colorEntitys.get(2)));
        graphEntitys.add(new GraphEntity("График 3", -20, 20, 0.5f, "3^x", colorEntitys.get(3)));
        graphEntitys.add(new GraphEntity("График 4", -20, 20, 0.5f, "5", colorEntitys.get(4)));
        graphEntitys.add(new GraphEntity("График 5", -20, 20, 0.5f, "", colorEntitys.get(5)));
        graphEntitys.add(new GraphEntity("График 6", -20, 20, 0.5f, "", colorEntitys.get(6)));
    }

    private void renderGraphs() {
        graphicsCanvas.clearCanvas();
        graphicsCanvas.resetCenterPoint();
        graphicsCanvas.drawAxis();
        for (int i = 0; i < graphEntitys.size(); i++) {
            final GraphEntity graphEntity = graphEntitys.get(i);
            if (graphEntity.getActive() && !graphEntity.getFormula().equals("")) {

                Float min = graphEntity.getMinX();
                Float max = graphEntity.getMaxX();
                Float step = graphEntity.getStep();

                final List<FloatPoint> pointList = new ArrayList<FloatPoint>();
                for (Float j = min; j < max; j += step) {
                    if (graphEntity.getFormula().indexOf("/x") > -1 && j == 0) {
                        break;
                    }
                    try {
                        Calculable calc = new ExpressionBuilder(graphEntity.getFormula()).withVariable("x", j).build();
                        float valueY = (float)((-1) * calc.calculate());
                        FloatPoint point = new FloatPoint(j, valueY);
                        pointList.add(point);
                    } catch (UnknownFunctionException ex) {
                        Logger.getLogger(Plotter2dApplet.class.getName()).log(Level.WARNING, null, ex);
                    } catch (UnparsableExpressionException ex) {
                        Logger.getLogger(Plotter2dApplet.class.getName()).log(Level.WARNING, null, ex);
                    }
                    graphicsCanvas.drawGraph(pointList, graphEntity.getColorEntity().getColor());
                }
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        graphicsCanvas = new plotter2d.GraphicsCanvas();
        jPanel6 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        minXSpinner = new javax.swing.JSpinner();
        maxXSpinner = new javax.swing.JSpinner();
        stepTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        activeCheckBox = new javax.swing.JCheckBox();
        colorComboBox = new javax.swing.JComboBox();
        formulaTextField = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        scaleXSpinner = new javax.swing.JSpinner();
        jLabel8 = new javax.swing.JLabel();
        scaleYSpinner = new javax.swing.JSpinner();
        clearButton = new javax.swing.JButton();
        drawButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        graphsList = new javax.swing.JList();
        deleteButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(204, 204, 255));

        graphicsCanvas.setBackground(new java.awt.Color(239, 239, 239));
        graphicsCanvas.setMaximumSize(new java.awt.Dimension(586, 446));
        graphicsCanvas.setMinimumSize(new java.awt.Dimension(586, 446));
        graphicsCanvas.setPreferredSize(new java.awt.Dimension(586, 446));

        javax.swing.GroupLayout graphicsCanvasLayout = new javax.swing.GroupLayout(graphicsCanvas);
        graphicsCanvas.setLayout(graphicsCanvasLayout);
        graphicsCanvasLayout.setHorizontalGroup(
            graphicsCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 586, Short.MAX_VALUE)
        );
        graphicsCanvasLayout.setVerticalGroup(
            graphicsCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 446, Short.MAX_VALUE)
        );

        jPanel6.setMaximumSize(new java.awt.Dimension(550, 160));
        jPanel6.setMinimumSize(new java.awt.Dimension(550, 160));
        jPanel6.setPreferredSize(new java.awt.Dimension(550, 160));

        jLabel9.setText("Имя");

        jLabel1.setText("Минимум по X");

        jLabel2.setText("Максимум по X");

        jLabel3.setText("Шаг");

        nameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nameTextFieldKeyReleased(evt);
            }
        });

        minXSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                minXSpinnerStateChanged(evt);
            }
        });

        maxXSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                maxXSpinnerStateChanged(evt);
            }
        });

        stepTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                stepTextFieldKeyReleased(evt);
            }
        });

        jLabel4.setText("Формула");

        jLabel5.setText("Цвет графика");

        jLabel6.setText("Активен (да/нет)");

        activeCheckBox.setText("Состояние");
        activeCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                activeCheckBoxItemStateChanged(evt);
            }
        });

        colorComboBox.setModel(new ComboBoxModel() {
            private ColorEntity currentColorEntity;

            @Override
            public void setSelectedItem(Object anItem) {
                currentColorEntity = (ColorEntity) anItem;
            }

            @Override
            public Object getSelectedItem() {
                return currentColorEntity;
            }

            @Override
            public int getSize() {
                return colorEntitys.size();
            }

            @Override
            public Object getElementAt(int index) {
                return colorEntitys.get(index);
            }

            @Override
            public void addListDataListener(ListDataListener l) {
                //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void removeListDataListener(ListDataListener l) {
                //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        });
        colorComboBox.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                colorComboBoxPropertyChange(evt);
            }
        });

        formulaTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formulaTextFieldKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(stepTextField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(minXSpinner, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(maxXSpinner, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(formulaTextField)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(activeCheckBox)
                            .addComponent(colorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(6, 6, 6))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(formulaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(colorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(activeCheckBox)))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(minXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(maxXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(stepTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel7.setMaximumSize(new java.awt.Dimension(242, 160));
        jPanel7.setMinimumSize(new java.awt.Dimension(242, 160));
        jPanel7.setPreferredSize(new java.awt.Dimension(242, 160));

        jLabel7.setText("Масштаб по X");

        scaleXSpinner.setValue(graphicsCanvas.getGraphScaleX());
        scaleXSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                scaleXSpinnerStateChanged(evt);
            }
        });

        jLabel8.setText("Масштаб по Y");

        scaleYSpinner.setRequestFocusEnabled(false);
        scaleYSpinner.setValue(graphicsCanvas.getGraphScaleY());
        scaleYSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                scaleYSpinnerStateChanged(evt);
            }
        });

        clearButton.setText("Очистить");
        clearButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                clearButtonMouseReleased(evt);
            }
        });

        drawButton.setText("Отрисовать");
        drawButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                drawButtonMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(scaleYSpinner)
                            .addComponent(scaleXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(78, 78, 78))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(drawButton))
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(scaleXSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(scaleYSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(clearButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(drawButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setMaximumSize(new java.awt.Dimension(194, 430));
        jPanel1.setMinimumSize(new java.awt.Dimension(194, 430));
        jPanel1.setPreferredSize(new java.awt.Dimension(194, 430));

        jScrollPane2.setMaximumSize(new java.awt.Dimension(190, 378));
        jScrollPane2.setMinimumSize(new java.awt.Dimension(190, 378));
        jScrollPane2.setPreferredSize(new java.awt.Dimension(190, 378));

        graphsList.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        graphsList.setModel(new javax.swing.AbstractListModel() {
            public int getSize() {
                return graphEntitys.size();
            }

            public Object getElementAt(int i) {
                return graphEntitys.get(i);
            }
        });
        graphsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        graphsList.setSelectedIndex(0);
        graphsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                graphsListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(graphsList);

        deleteButton.setText("Удалить");
        deleteButton.setMargin(new java.awt.Insets(2, 10, 2, 10));
        deleteButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                deleteButtonMouseReleased(evt);
            }
        });

        addButton.setText("Добавить");
        addButton.setMargin(new java.awt.Insets(2, 10, 2, 10));
        addButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                addButtonMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addButton)
                    .addComponent(deleteButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(graphicsCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, 538, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(graphicsCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void graphsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_graphsListValueChanged
        // TODO add your handling code here:
        if (graphsList.getSelectedIndex() > -1) {
            GraphEntity graphEntity = this.graphEntitys.get(graphsList.getSelectedIndex());
            nameTextField.setText(graphEntity.getName());
            minXSpinner.setValue(graphEntity.getMinX());
            maxXSpinner.setValue(graphEntity.getMaxX());
            stepTextField.setText("" + graphEntity.getStep());
            formulaTextField.setText("" + graphEntity.getFormula());
            colorComboBox.getModel().setSelectedItem(graphEntity.getColorEntity());
            colorComboBox.repaint();
            activeCheckBox.setSelected(graphEntity.getActive());
        }
    }//GEN-LAST:event_graphsListValueChanged
    
    private void nameTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameTextFieldKeyReleased
        // TODO add your handling code here:
        if (graphsList.getSelectedIndex() > -1) {
            graphEntitys.get(graphsList.getSelectedIndex())
                    .setName(nameTextField.getText());
            graphsList.repaint();
        }
    }//GEN-LAST:event_nameTextFieldKeyReleased
    
    private void minXSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_minXSpinnerStateChanged
        // TODO add your handling code here:
        if (graphsList.getSelectedIndex() > -1) {
            graphEntitys.get(graphsList.getSelectedIndex())
                    .setMinX((Float) minXSpinner.getValue());
        }
    }//GEN-LAST:event_minXSpinnerStateChanged

    private void maxXSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_maxXSpinnerStateChanged
        // TODO add your handling code here:
        if (graphsList.getSelectedIndex() > -1) {
            graphEntitys.get(graphsList.getSelectedIndex())
                    .setMaxX((Float) maxXSpinner.getValue());
        }
    }//GEN-LAST:event_maxXSpinnerStateChanged

    private void stepTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_stepTextFieldKeyReleased
        // TODO add your handling code here:
        if (graphsList.getSelectedIndex() > -1) {
            graphEntitys.get(graphsList.getSelectedIndex())
                    .setStep(Float.parseFloat(stepTextField.getText()));
        }
        //renderGraphs();
    }//GEN-LAST:event_stepTextFieldKeyReleased

    private void formulaTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formulaTextFieldKeyReleased
        // TODO add your handling code here:
        if (graphsList.getSelectedIndex() > -1) {
            graphEntitys.get(graphsList.getSelectedIndex())
                    .setFormula(formulaTextField.getText());
        }
        //renderGraphs();
    }//GEN-LAST:event_formulaTextFieldKeyReleased

    private void colorComboBoxPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_colorComboBoxPropertyChange
        // TODO add your handling code here:
        if (graphsList.getSelectedIndex() > -1 && colorComboBox.getSelectedIndex() > -1) {
            graphEntitys.get(graphsList.getSelectedIndex())
                    //.setColorEntity(colorEntitys.get(colorComboBox.getSelectedIndex()));
                    .setColorEntity((ColorEntity) colorComboBox.getSelectedItem());
        }
        //renderGraphs();
    }//GEN-LAST:event_colorComboBoxPropertyChange

    private void scaleXSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_scaleXSpinnerStateChanged
        // TODO add your handling code here:
        if ((Integer) scaleXSpinner.getValue() < 0) {
            scaleXSpinner.setValue(0);
        }
        graphicsCanvas.setGraphScaleX((Integer) scaleXSpinner.getValue());
        //renderGraphs();
    }//GEN-LAST:event_scaleXSpinnerStateChanged

    private void scaleYSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_scaleYSpinnerStateChanged
        // TODO add your handling code here:
        if ((Integer) scaleYSpinner.getValue() < 0) {
            scaleYSpinner.setValue(0);
        }
        graphicsCanvas.setGraphScaleY((Integer) scaleYSpinner.getValue());
        //renderGraphs();
    }//GEN-LAST:event_scaleYSpinnerStateChanged

    private void activeCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_activeCheckBoxItemStateChanged
        // TODO add your handling code here:
        if (graphsList.getSelectedIndex() > -1) {
            graphEntitys.get(graphsList.getSelectedIndex())
                    .setActive(activeCheckBox.isSelected());
        }
        //renderGraphs();
    }//GEN-LAST:event_activeCheckBoxItemStateChanged

    private void clearButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clearButtonMouseReleased
        // TODO add your handling code here:
        graphicsCanvas.clearCanvas();
        graphicsCanvas.drawAxis();
    }//GEN-LAST:event_clearButtonMouseReleased

    private void drawButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_drawButtonMouseReleased
        // TODO add your handling code here:
        renderGraphs();
    }//GEN-LAST:event_drawButtonMouseReleased

    private void deleteButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_deleteButtonMouseReleased
        // TODO add your handling code here:
        if (graphsList.getSelectedIndex() > -1) {
            graphEntitys.remove(graphsList.getSelectedIndex());
        }
        graphsList.repaint();
    }//GEN-LAST:event_deleteButtonMouseReleased

    private void addButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addButtonMouseReleased
        // TODO add your handling code here:
        graphEntitys.add(
                new GraphEntity("График -", -20, 20, 0.5f, "", colorEntitys.get(graphEntitys.size() % colorEntitys.size() - 1)));
        graphsList.repaint();
    }//GEN-LAST:event_addButtonMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox activeCheckBox;
    private javax.swing.JButton addButton;
    private javax.swing.JButton clearButton;
    private javax.swing.JComboBox colorComboBox;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton drawButton;
    private javax.swing.JTextField formulaTextField;
    private plotter2d.GraphicsCanvas graphicsCanvas;
    private javax.swing.JList graphsList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSpinner maxXSpinner;
    private javax.swing.JSpinner minXSpinner;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JSpinner scaleXSpinner;
    private javax.swing.JSpinner scaleYSpinner;
    private javax.swing.JTextField stepTextField;
    // End of variables declaration//GEN-END:variables
}
